#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <graphics.h>

 int x31,x32,y31,y32;

 void rect1 (int x1, int y1, int x2, int y2)
 {   setcolor(1);
   rectangle(x1,y1,x2,y2);
    setcolor(14);
   arc(x1+y2-y1,y2,90,180,y2-y1);
       }

 void rect2 (int x1, int y1, int x2, int y2)
 {   setcolor(1);
   rectangle(x1,y1,x2,y2);
     setcolor(14);
   arc(x1,y1+x2-x1,0,90,x2-x1);
   }

  void rect3 (int x1, int y1, int x2, int y2)
 {   setcolor(1);
   rectangle(x1,y1,x2,y2);
     setcolor(14);
   arc(x2-y2+y1,y1,270,360,y2-y1);
   }

 void rect4 (int x1, int y1, int x2, int y2)
 {   setcolor(1);
   rectangle(x1,y1,x2,y2);
     setcolor(14);
   arc(x2,y2-x2+x1,180,270,x2-x1);
   }

 void gold(int n)
 {  int i,j,k,l;
   for(i=1;i<n;i++)
    { if (i%4==0) if ((x32>x31)&&(y32>y31))
          { rect1(x31,y31,x32,y32-x32+x31); y32=y32-x32+x31; }
           else break;
      if (i%4==1) if ((x32>x31)&&(y32>y31))
          { rect2(x31+y32-y31,y31,x32,y32); x31=x31+y32-y31; }
           else break;
      if (i%4==2) if ((x32>x31)&&(y32>y31))
          { rect3(x31,y31+x32-x31,x32,y32); y31=y31+x32-x31; }
           else break;
      if (i%4==3) if ((x32>x31)&&(y32>y31))
          { rect4(x31,y31,x32-y32+y31,y32); x32=x32-y32+y31; }
           else break;
            } }

 void main()
 { int n;
  int gdriver=DETECT,gmode;
   initgraph(&gdriver,&gmode,"");
  x31=10;
  y31=20;
  x32=625;
  y32=400;
  cout << "Introdu numarul de dreptunghiuri pe care sa le desenez : ";
  cin >> n;
  cleardevice();
  setbkcolor(0);
  rect1(x31,y31,x32,y32);
  gold(n);
  getch();
  closegraph();
         }
